package com.aierkeji.call.b2c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.aierkeji.call.b2c.util.GsonUtils;
import com.aierkeji.call.b2c.util.ResultMapHelper;

/***
 *  author:james.xu
 *  主要用来处理框架异常，比如shiro有自己的拦截器，不会抛到全局异常处理器。现在这里处理。
 */

public abstract class BaseController {

    private final Logger log = LogManager.getLogger(getClass());


    /**
     * 权限异常
     */
    @ExceptionHandler({ UnauthorizedException.class, AuthorizationException.class })
    public void authorizationException(HttpServletResponse response) {
        try(PrintWriter out = response.getWriter()){
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            out.write(GsonUtils.parseToJson(ResultMapHelper.permissionDenied()));
            return;
        }catch (IOException e) {
            log.error("未知异常：",e);
        }
    }

}
